package com.lsh.controller;

import com.lsh.domain.LoginUser;
import com.lsh.service.LoginNewService;
import com.lsh.util.result.Result;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * 登录账号模块
 * @author Administrator
 * @date 2019/10/07 19:29
 */
@Controller
@RequestMapping("/loginnew")
public class LoginNewController {

    @Autowired
    private LoginNewService loginNewService;

    private Logger log = Logger.getLogger(this.getClass());
    /**
     * 跳转到登录页面
     * @return
     */
    @RequestMapping("/index")
    public String index(){
        return "loginnew";
    }

    /**
     * 登录
     * @param loginUser
     * @param bindingResult
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/login")
    @ResponseBody
    public Result login(@Validated @RequestBody LoginUser loginUser, BindingResult bindingResult,
                        HttpServletRequest request, HttpServletResponse response){
        HashMap<String, Object> data = new HashMap<>();
        if(bindingResult.hasErrors()){
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            for(ObjectError error: allErrors){
                data.put("error_up", error.getDefaultMessage());
            }
            return new Result(1002, "登录失败", data);
        }
        String checkCode = loginNewService.checkCaptcha(loginUser.getCode(), loginUser.getcToken());
        String checkUser = loginNewService.checkUser(loginUser.getUsername(), loginUser.getPassword());
        if(checkCode != null || checkUser == null){
            data.put("error_code", checkCode);
            if(checkUser == null) data.put("error_up", "用户名或密码错误");
            return new Result(1002, "登录失败", data);
        }else{
            String token = loginNewService.resToken(checkUser, loginUser.getUsername());
            if(token == null) {
                data.put("error_up", "令牌获取失败");
                return new Result(1002, "登录失败", data);
            } else{ //返回令牌回客户端
                addCookie(token, request, response);
                return new Result(1001, "登录成功");
            }
        }
    }

    /**
     * 把令牌添加进cookie
     * @param token
     * @param request
     * @param response
     */
    public static void addCookie(String token, HttpServletRequest request, HttpServletResponse response){
        Cookie cookie = new Cookie("TOKEN", token);
        cookie.setPath(request.getContextPath() + "/");
        cookie.setMaxAge(365*30*30*60);
        response.addCookie(cookie);
    }
}
